package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.common.exception.LeadNewsException;
import com.heima.model.admin.dto.AdChannelDTO;
import com.heima.model.admin.pojo.AdChannel;
import com.heima.model.common.dto.PageResponseResult;
import com.heima.model.common.dto.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author Ronaldo
 * @date 2021/7/29
 */
@Service
@Transactional
@Slf4j
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    @Override
    public ResponseResult queryPage(AdChannelDTO channelDTO) {

        LambdaQueryWrapper<AdChannel> adChannelLambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNoneBlank(channelDTO.getName())) {
            adChannelLambdaQueryWrapper.like(AdChannel::getName, channelDTO.getName());
        }

        //分页模型
        IPage<AdChannel> page = new Page<>(channelDTO.getPage(), channelDTO.getSize());

        page = this.page(page, adChannelLambdaQueryWrapper);

        PageResponseResult pageResponseResult = new PageResponseResult(channelDTO.getPage(), channelDTO.getSize(), (int) page.getTotal());

        List<AdChannel> adChannelList = page.getRecords();
        log.info("Queried data {}" + adChannelList);
        if (CollectionUtils.isEmpty(adChannelList)) {
            throw new LeadNewsException(AppHttpCodeEnum.NOT_FOUND_CHANNEL);
        }
        pageResponseResult.setData(adChannelList);

        return pageResponseResult;
    }

    @Override
    public ResponseResult saveAdChannel(AdChannel adChannel) {

        adChannel.setCreatedTime(new Date());
        adChannel.setStatus(true);

        boolean saveResult = this.save(adChannel);
        log.info("insert AdChannel result {}" + adChannel);
        if (!saveResult){
            throw new LeadNewsException(AppHttpCodeEnum.SERVER_ERROR);
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }

    @Override
    public ResponseResult updateAdChannel(AdChannel adChannel) {

        AdChannel channel = this.getById(adChannel.getId());
        log.info("select AdChannel {}" + channel);
        if (Objects.isNull(channel)){
            throw new LeadNewsException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        boolean updateResult = this.updateById(adChannel);
        log.info("update AdChannel result {}" + updateResult);
        if (!updateResult){
            throw new LeadNewsException(AppHttpCodeEnum.SERVER_ERROR);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deleteAdChannelById(Integer id) {

        AdChannel adChannel = this.getById(id);
        log.info("select channel by id {} " + id);

        if (Objects.isNull(adChannel)){
            throw new LeadNewsException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if (adChannel.getStatus()){
            throw new LeadNewsException(AppHttpCodeEnum.CHANNEL_STATUS_IS_TRUE);
        }

        boolean removeResult = this.removeById(id);
        log.info("delete result {}" + removeResult);
        if (!removeResult){
            throw new LeadNewsException(AppHttpCodeEnum.SERVER_ERROR);
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
